#!/usr/bin/env bash

if [ $(id -u) != '0' ]; then
    echo 'error: script needs root user.'
    exit 1
fi

NGINX_VERSION='1.11.8'
PHP_VERSION='7.0.14'
PERCONA_VERSION='5.6.34-79.1'
REDIS_VERSION='3.2.6'
PCRE_VERSION='8.39'
ZLIB_VERSION='1.2.8'
OPENSSL_VERSION='1.0.2j'
#LIBXML2_VERSION='2.9.3'
#LIBZIP_VERSION='1.1.3'
LIBGD_VERSION='2.2.3'
LIBCURL_VERSION='7.52.1'

CERTBOT_VERSION='0.9.3'

CPU_NUM=`cat /proc/cpuinfo | grep processor | wc -l`

yum -y update

yum -y install gcc \
gcc-c++ \
automake \
autoconf \
libtool \
make \
cmake \
zlib \
zlib-devel \
pcre \
pcre-devel \
freetype \
freetype-devel \
libpng \
libpng-devel \
libevent \
libevent-devel \
libmcrypt \
libmcrypt-devel \
libjpeg \
libjpeg-devel \
jemalloc \
jemalloc-devel \
libxml2 \
libxml2-devel \
bzip2 \
bzip2-devel \
libcurl \
libcurl-devel \
glibc \
glibc-devel \
glib2 \
glib2-devel \
openssl \
openssl-devel \
bison \
bison-devel \
ncurses \
ncurses-devel \
gd \
gd-devel \
flex \
flex-devel \
libwebp \
libwebp-devel \
gettext \
gettext-devel \
gmp \
gmp-devel \
libaio \
libaio-devel \
readline \
readline-devel \
recode \
recode-devel \
libxslt \
libxslt-devel \
icu \
libicu \
libicu-devel \
libzip \
libzip-devel \
p7zip

dd if=/dev/zero of=/home/swap bs=1024 count=1024000
mkswap /home/swap
swapon /home/swap

groupadd webmaster
groupadd mysql

useradd -g webmaster -M -d /usr/local/nginx webmaster -s /sbin/nologin
useradd -g mysql -M -d /usr/local/mysql mysql -s /sbin/nologin

cd /usr/local/src/fast-server

wget http://mirrors.sohu.com/nginx/nginx-${NGINX_VERSION}.tar.gz
wget http://cn2.php.net/distributions/php-${PHP_VERSION}.tar.gz
wget https://www.percona.com/downloads/Percona-Server-5.6/Percona-Server-${PERCONA_VERSION}/source/tarball/percona-server-${PERCONA_VERSION}.tar.gz
wget http://download.redis.io/releases/redis-${REDIS_VERSION}.tar.gz

wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz
#wget https://nih.at/libzip/libzip-${LIBZIP_VERSION}.tar.gz
#wget ftp://xmlsoft.org/libxml2/libxml2-${LIBXML2_VERSION}.tar.gz
wget https://github.com/libgd/libgd/releases/download/gd-${LIBGD_VERSION}/libgd-${LIBGD_VERSION}.tar.gz
wget https://curl.haxx.se/download/curl-${LIBCURL_VERSION}.tar.gz

tar -zxvf nginx-${NGINX_VERSION}.tar.gz
tar -zxvf php-${PHP_VERSION}.tar.gz
tar -zxvf percona-server-${PERCONA_VERSION}.tar.gz
tar -zxvf redis-${REDIS_VERSION}.tar.gz

tar -zxvf openssl-${OPENSSL_VERSION}.tar.gz
#tar -zxvf libzip-${LIBZIP_VERSION}.tar.gz
#tar -zxvf libxml2-${LIBXML2_VERSION}.tar.gz
tar -zxvf libgd-${LIBGD_VERSION}.tar.gz
tar -zxvf curl-${LIBCURL_VERSION}.tar.gz

cd /usr/local/src/fast-server/lib
tar -zxvf pcre-${PCRE_VERSION}.tar.gz
tar -zxvf zlib-${ZLIB_VERSION}.tar.gz

# install nginx
cd /usr/local/src/fast-server/nginx-${NGINX_VERSION}
./configure --prefix=/usr/local/nginx \
--user=webmaster \
--group=webmaster \
--with-pcre=/usr/local/src/fast-server/lib/pcre-${PCRE_VERSION} \
--with-zlib=/usr/local/src/fast-server/lib/zlib-${ZLIB_VERSION} \
--with-openssl=/usr/local/src/fast-server/openssl-${OPENSSL_VERSION} \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-http_degradation_module
#--with-ld-opt="-ljemalloc"
#--with-pcre-jit \
#--with-ipv6 \
#--with-http_random_index_module \

make -j${CPU_NUM} && make install

#mkdir /usr/local/nginx/html/yaconf
mkdir /usr/local/nginx/html/certbot
mkdir /usr/local/nginx/html/certbot-configs
mkdir -p /usr/local/nginx/html/logs/cronjob

# nginx.conf
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
cp /usr/local/src/fast-server/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
mv /usr/local/src/fast-server/conf/vhosts /usr/local/nginx/conf/vhosts
# certbot
cp /usr/local/src/fast-server/conf/certbot.conf /usr/local/nginx/html/certbot-configs/certbot.conf

chown -R webmaster:webmaster /usr/local/nginx

# install packages
#cd /usr/local/src/fast-server/libxml2-${LIBXML2_VERSION}
#./configure prefix=/usr/local/libxml2
#make -j${CPU_NUM} && make install

#cd /usr/local/src/fast-server/libzip-${LIBZIP_VERSION}
#./configure prefix=/usr/local/libzip
#make -j${CPU_NUM} && make install

#cd /usr/local/src/fast-server/openssl-${OPENSSL_VERSION}
#./config prefix=/usr/local/openssl
#make -j${CPU_NUM} && make install

cd /usr/local/src/fast-server/libgd-${LIBGD_VERSION}
./configure --prefix=/usr/local/libgd
make -j${CPU_NUM} && make install

cd /usr/local/src/fast-server/curl-${LIBCURL_VERSION}
./configure --prefix=/usr/local/curl
make -j${CPU_NUM} && make install

cd /usr/local/src/fast-server/lib/pcre-${PCRE_VERSION}
./configure --prefix=/usr/local/pcre
make -j${CPU_NUM} && make install

cd /usr/local/src/fast-server/lib/zlib-${ZLIB_VERSION}
./configure --prefix=/usr/local/zlib
make -j${CPU_NUM} && make install


# install php
cd /usr/local/src/fast-server/php-${PHP_VERSION}
./configure --prefix=/usr/local/php \
--enable-fpm \
--with-fpm-user=webmaster \
--with-fpm-group=webmaster \
--with-libxml-dir=/usr/local/libxml2 \
--with-openssl \
--with-zlib=/usr/local/zlib \
--with-pcre-regex=/usr/local/pcre \
--with-pcre-jit \
--with-bz2 \
--with-curl=/usr/local/curl \
--with-icu-dir=/usr \
--with-gd=/usr/local/libgd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-webp-dir=/usr \
--with-freetype-dir=/usr \
--with-gettext \
--with-gmp \
--with-mhash \
--with-mcrypt \
--with-xmlrpc \
--with-mysqli \
--with-pdo-mysql \
--with-readline \
--with-recode \
--with-iconv \
--with-xsl \
--without-pear \
--enable-libxml \
--enable-bcmath \
--enable-calendar \
--enable-ctype \
--enable-dba \
--enable-dom \
--enable-exif \
--enable-intl \
--enable-fileinfo \
--enable-filter \
--enable-ftp \
--enable-gd-native-ttf \
--enable-json \
--enable-mbstring \
--enable-mysqlnd \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--enable-xml \
--enable-xmlreader \
--enable-zip \
--enable-opcache \
--enable-soap \
--enable-sockets \
--enable-wddx \
--disable-phar \
--disable-short-tags
# --with-libdir=lib64 \
# --with-tidy \
# --with-fpm-systemd \
# --with-libzip \

make -j${CPU_NUM} && make install

# php conf
cp /usr/local/src/fast-server/conf/php-fpm.conf  /usr/local/php/etc/php-fpm.conf
cp /usr/local/src/fast-server/conf/www.conf      /usr/local/php/etc/php-fpm.d/www.conf
cp /usr/local/src/fast-server/conf/php.ini       /usr/local/php/lib/php.ini

chown -R webmaster:webmaster /usr/local/php

# install percona-server 5.6
cd /usr/local/src/fast-server/percona-server-${PERCONA_VERSION}
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/servdata \
-DTMPDIR=/usr/local/mysql/servtmp \
-DSYSTEMD_PID_DIR=/usr/local/mysql/servlog \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DENABLED_LOCAL_INFILE=1 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_unicode_ci \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_LIBEVENT=bundled \
-DWITH_SSL=bundled \
-DWITH_ZLIB=bundled

# not used: DSYSTEMD_PID_DIR DWITH_XTRADB_STORAGE_ENGINE

make -j${CPU_NUM} && make install

mkdir /usr/local/mysql/servdata
mkdir /usr/local/mysql/servtmp
mkdir /usr/local/mysql/servlog

# mysql conf
mv /etc/my.cnf /etc/my.cnf.bakup
cp /usr/local/src/fast-server/conf/my-percona-56.cnf /etc/my.cnf

chown -R mysql:mysql /usr/local/mysql

# initialize mysql data @see https://dev.mysql.com/doc/refman/5.6/en/mysql-install-db.html
/usr/local/mysql/scripts/mysql_install_db --user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/servdata

# add mysqld service
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

chown -R mysql:mysql /usr/local/mysql

# install redis
cd /usr/local/src/fast-server/redis-${REDIS_VERSION}
make -j${CPU_NUM} && make PREFIX=/usr/local/redis install

mkdir /usr/local/redis/conf
cp /usr/local/src/fast-server/conf/redis.conf /usr/local/redis/conf/redis.conf

# swap off
#swapoff /home/swap
#rm -rf /home/swap

# start all service
/usr/local/nginx/sbin/nginx
/usr/local/php/sbin/php-fpm
service mysqld start

# access
chown -R webmaster:webmaster /usr/local/redis
chown -R webmaster:webmaster /usr/local/nginx
chown -R webmaster:webmaster /usr/local/php
chown -R mysql:mysql /usr/local/mysql

# password
/usr/local/mysql/bin/mysqladmin -uroot password abcd

# certbot
cd /usr/local/src/fast-server
wget https://github.com/certbot/certbot/archive/v${CERTBOT_VERSION}.tar.gz
tar -zxvf v${CERTBOT_VERSION}.tar.gz

chmod +x /usr/local/src/fast-server/certbot-${CERTBOT_VERSION}/certbot-auto
#/usr/local/src/fast-server/certbot-${CERTBOT_VERSION}/certbot-auto
